package com.sindvision.cces.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = GeneralException.class)
    public ApiResultVO jsonErrorHandler(GeneralException e) {
        return ApiResult.error(e.getCode(), e.getInfo());
    }

    @ExceptionHandler(value = org.springframework.validation.BindException.class)
    public ApiResultVO bindException(org.springframework.validation.BindException e) {
        final FieldError fieldError = e.getFieldError();
        final String defaultMessage = fieldError.getDefaultMessage();
        return ApiResult.error("400", defaultMessage);
    }

    @ExceptionHandler(value = org.springframework.web.bind.MissingServletRequestParameterException.class)
    public ApiResultVO missingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException e) {
        final String parameterName = e.getParameterName();
        return ApiResult.error("400", "缺少参数：" + parameterName);
    }
}
